package FileClass;



import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

public class SplitFile
{
	
	public boolean splitFile(String src, String dest, int parts)
	{
		File file = new File(src);
		if(!file.exists() || file.isDirectory())
			return false;
		try
		{
			DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
			DataOutputStream dos = null;
			long partSize = file.length()/parts;
			for(int i = 0 ; i<parts; i++)
			{ 
				
				dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dest + "\\" +file.getName() +".00" + (i+1))));
				int dataRead;
				byte[] buffer = new byte[102400];
				long outSize = 0;
				while((dataRead = dis.read(buffer))!=-1)
				{
					dos.write(buffer, 0, dataRead);
					outSize += buffer.length;
					if(outSize >= partSize)
						break;
				}
				dos.close();
			}
			dis.close();
		}catch(Exception ex)
		{
			ex.printStackTrace();
			return false;
		}
		return true;
	}
	//
	public boolean joinFile(String file001, String dest)
	{
		File file = new File(file001);
		if(!file.exists() || file.isDirectory())
			return false;
		try
		{
			
			File[] listFile = getListFile(file);
			DataInputStream dis = null;
			String fileOutPath = dest +"\\"+file.getName().substring(0,file.getName().lastIndexOf(".001"));
			DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileOutPath)));
			for(File item : listFile)
			{
				dis = new DataInputStream(new BufferedInputStream(new FileInputStream(item)));
				int read;
				byte[] buffer = new byte[10240];
				while((read = dis.read(buffer))!=-1)
				{
					dos.write(buffer, 0, read);
				}
				dis.close();
			}
			dos.close();
		}catch(Exception ex)
		{
			ex.printStackTrace();
			return false;
		}
		return true;
	}
	
	private File[] getListFile(File file001)
	{
		File[] listFile = new File(file001.getParent()).listFiles();
		List<File> listFileOut = new ArrayList<>();
		
		String file001Name = file001.getName().substring(0,file001.getName().lastIndexOf(".001"));
		
		for(File file : listFile)
		{
			String fileName = file.getName().substring(0,file.getName().length()-4);
			if(file001Name.equals(fileName))
				listFileOut.add(file);
		}
		
		listFile = new File[listFileOut.size()];
		for(int i = 0 ; i<listFile.length; i++)
		{
			listFile[i] = listFileOut.get(i);
		}
		return listFile;
	}
	
	public static void main(String[] args)
	{
		SplitFile s = new SplitFile();
//		String src = "D:\\IDM 6.15\\IDMan.exe";
//		String dest = "D:\\IDM 6.15\\CatFile";
//		int parts = 5;
//		System.out.println(s.splitFile(src, dest, parts));
//		---------------------
		String file001 = "D:\\IDM 6.15\\CatFile";
		String dest = "D:\\IDM 6.15\\NoiFile";
		System.out.println(s.joinFile(file001, dest));
	}
}